/*

Input: newdata/estimation_prep_ltw18 [prepared precinct-level panel]

Output: Figure 8, 9

Task: Heterogeneity by Increase/Decrease Distance (2 and 3 distance bins)

*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
	
********************************************************************************
*	 Prep Estimation *
********************************************************************************
				
	// relabel outcomes 
	lab var turnout_urne 	"Polling place turnout"
	lab var turnout_pos_req "Mail-in turnout"
	lab var turnout_tot_req "Total turnout"
		
	// compute group ids for DISTANCE increase/decrease, 0 else
	cap drop tmp*
	assert del_street_dist!=0 if K==0
	gen 	tmp = (del_street_dist>0) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase in event, 0 else"
	
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease in event, 0 else"
	
	tab ind_dist_up ind_dist_dn if K==0	
	
	
	// compute group ids for DISTANCE: 3 groups
	cap drop tmp*
	_pctile del_street_dist							if K==0 & del_street_dist<.7, n(3)
	gen 	tmp1 = (del_street_dist>r(r2)) 			if K==0
	bys sb_new (tmp1): gen ind_dist_m3 = tmp1[1]
	replace ind_dist_m3 = 0 						if missing(Ei)
	
	gen 	tmp2 = (inrange(del_street_dist,r(r1),r(r2))) if K==0
	bys sb_new (tmp2): gen ind_dist_m2 = tmp2[1]						
	replace ind_dist_m2 = 0 						if missing(Ei)
	
	gen 	tmp3 = (del_street_dist<r(r1)) 			if K==0
	bys sb_new (tmp3): gen ind_dist_m1 = tmp3[1]
	replace ind_dist_m1 = 0 						if missing(Ei)
	
	su del_street_dist if ind_dist_m1 & K==0
	su del_street_dist if ind_dist_m2 & K==0
	su del_street_dist if ind_dist_m3 & K==0
	
	
********************************************************************************
*	 Heterogeneity: Increase/Decrease Distance (2 bins), Figure 8
********************************************************************************	
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_dn				// a := decrease
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b = F`l'event *ind_dist_up				// b:= increase
		lab var F`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b+F`l'event_a==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_dn	// a := decrease
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b = L`l'event *ind_dist_up	// b:= increase
		lab var L`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		assert  L`l'event_b+L`l'event_a==L`l'event
	}		
	
	// ORDER dummies
	order *event_b, last
	order F1event*,last	
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg,clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req del_street_dist treat_simple{
	
			reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b ///
				$ctr $wgt if smpl_trim ==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
				qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b) store(`v')

		estimates store `v'_a
		estimates store `v'_b	
		
		// PLOT
		event_plot  `v'_a `v'_b , ///
		stub_lag(L#event_a L#event_b ) stub_lead(F#event_a F#event_b ) plottype(connect) ciplottype(rcap) ///
		together perturb(-0.1(0.2)0.1) trimlead(4) trimlag(2) noautolegend ///
		graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment") ///
			legend(pos(12) order(1 "Distance decrease" 3 "Distance increase" ) row(1) region(style(none))) ///
			xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lpat(solid) lcol(gray)) ylabel(, angle(horizontal)) ///
			name(`v', replace)) ///
		lag_opt1(msymbol(O) msize(2.5pt) color(black) 	  lpat(-)) 	lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(S) msize(2.5pt) color(cranberry) lpat(solid)) lag_ci_opt2(color(cranberry)) 	 		 	
	}
	
	
	// auxillary tests of L1event-L0event for mail-in and in-person when dist increased (test: waning vs. inattentive)
	qui estimates restore turnout_pos_req_a
	qui lincom L1event_b-L0event_b
	di stritrim("Mail-in: tau1-tau0:`:di %12.2f `r(estimate)'', p=`:di %12.3f `r(p)''") 
	qui estimates restore turnout_urne_a
	qui lincom L1event_b-L0event_b
	di stritrim("In-person: tau1-tau0:`:di %12.2f `r(estimate)'', p=`:di %12.3f `r(p)''") 
		
	
	* PLOT: FIGURE 8. Effect Heterogeneity by Change in Proximity to the Polling Location
	grc1leg del_street_dist turnout_urne turnout_pos_req turnout_tot_req, col(2)  xcommon pos(6) imargins(small)
	gr_edit .plotregion1.graph1.yaxis1.reset_rule -0.4 0.4 .2 , tickset(major) ruletype(range) 
	gr_edit .plotregion1.graph1.yaxis1.title.text = {}
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"Change in distance in km"'
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"(estimates)"'
		gr_edit .plotregion1.graph1.title.text = {}
		gr_edit .plotregion1.graph1.title.text.Arrpush {bf:Panel A.} Change in Distance
		gr_edit .plotregion1.graph2.title.text = {}
		gr_edit .plotregion1.graph2.title.text.Arrpush {bf:Panel B.} Effect on Polling Place Turnout
		gr_edit .plotregion1.graph3.title.text = {}
		gr_edit .plotregion1.graph3.title.text.Arrpush {bf:Panel C.} Effect on Mail-in Turnout
		gr_edit .plotregion1.graph4.title.text = {}
		gr_edit .plotregion1.graph4.title.text.Arrpush {bf:Panel D.} Effect on Total Turnout	
	gr_edit .style.editstyle declared_ysize(4) editcopy	
	gr_edit .plotregion1.graph1.plotregion1.plot3.style.editstyle line(pattern(blank)) editcopy
	gr_edit .plotregion1.graph1.plotregion1.plot1.style.editstyle line(pattern(blank)) editcopy
	graph export "$figures/Figure_8_ES_het_by_distance.pdf", replace	
	

	
********************************************************************************
*	 Heterogeneity by distance, 3 bins, Figure 9
********************************************************************************		

	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	
	// Create two set of dummies: Reason Dummy x rel. time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_m1	// a := decrease
		gen	 	F`l'event_b = F`l'event *ind_dist_m2	// b:= middle
		gen 	F`l'event_c = F`l'event *ind_dist_m3	// c := increase
		assert  F`l'event_b+F`l'event_a+F`l'event_c==F`l'event		
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		lab var F`l'event_b "(N0)x\hspace{.7cm}Reassignment (#t-`l'#)"
		lab var F`l'event_c "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"
		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_m1	// a := decrease
		gen	 	L`l'event_b = L`l'event *ind_dist_m2	// b:= middle
		gen 	L`l'event_c = L`l'event *ind_dist_m3	// c := increase
		assert  L`l'event_b+L`l'event_a+L`l'event_c==L`l'event	
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		lab var L`l'event_b "(N0)x\hspace{.7cm}Reassignment (#t+`l'#)"
		lab var L`l'event_c "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"		
		
	}		
	
	// ORDER dummies
	order *event_b, last
	order *event_c, last
	order F1event*,last	
	
	lab var turnout_urne 	"{bf:Panel B.} Effect on Polling Place Turnout"
	lab var turnout_pos_req "{bf:Panel C.} Effect on Mail-in Turnout"
	lab var turnout_tot_req "{bf:Panel D.} Effect on Total Turnout"
	lab var del_street_dist "{bf:Panel A.} Change in Distance"	
	
	
	// Estimate ES by increase/decrease in distance
	estimates clear
	outreg, clear
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req del_street_dist treat_simple{
	
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F7event_c-L7event_c F1event_a F1event_b F1event_c ///
				$ctr $wgt if smpl_trim ==1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)		
				qui outreg,  $opt  keep(F4event_a-L2event_a  F4event_b-L2event_b F4event_c-L2event_c) store(`v')

		estimates store `v'_a
		estimates store `v'_b	
		estimates store `v'_c	

		// Plot
		event_plot  `v'_a `v'_b `v'_c, ///
		stub_lag(L#event_a L#event_b L#event_c ) stub_lead(F#event_a F#event_b F#event_c) plottype(scatter) ciplottype(rcap) ///
		together perturb(-0.15(0.15)0.15) trimlead(4) trimlag(2) noautolegend ///
		graph_opt(xtitle("") ytitle("Voter turnout in %""(estimates)", size(small)) xlabel(-4(1)2) xtitle("Election since reassignment") ///
			legend(pos(12) order(1 "Distance decrease" 3 "Little distance change" 5 "Distance increase" ) row(1) region(style(none)) bexpand span justification(left)) ///
			xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lpat(solid) lcol(gray)) ylabel(, angle(horizontal)) ///
			title("`:var lab `v''", nobox span bexpand justification(left) size(medium)) ///
			name(`v', replace)) ///
		lag_opt1(msymbol(O) msize(2.5pt) color(black)) 	lag_ci_opt1(color(black)) ///
		lag_opt2(msymbol(Dh) msize(2.5pt) color(gray)) lag_ci_opt2(color(gray)) ///
		lag_opt3(msymbol(S) msize(2.5pt) color(cranberry)) lag_ci_opt3(color(cranberry)) 	 		 	

	}
	
	* 	PLOT: FIGURE 9. Effect Heterogeneity by Change in Proximity to the Polling Location
	grc1leg del_street_dist turnout_urne turnout_pos_req turnout_tot_req, col(2)  xcommon pos(6) imargins(small)
	gr_edit .plotregion1.graph1.yaxis1.reset_rule -0.6 0.6 .3 , tickset(major) ruletype(range) 
	gr_edit .plotregion1.graph1.yaxis1.title.text = {}
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"Change in distance in km"'
	gr_edit .plotregion1.graph1.yaxis1.title.text.Arrpush `"(estimates)"'
	gr_edit .style.editstyle declared_ysize(4) editcopy
	graph export "$figures/Figure_9_ES_het_by_distance_3clust.pdf", replace	
